package ch.hsr.roboProject.roboCam;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SearchDevices extends Activity {

	private ListView lv1;
	private String lv_arr[] = { "Device0", "Device1", "Device2", "etc.." };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchdevices);
		final ArrayAdapter<String> mArrayAdapter = null;

		// ####################################################
		 // Create a BroadcastReceiver for ACTION_FOUND
		 final BroadcastReceiver mReceiver = new BroadcastReceiver() {
		 public void onReceive(Context context, Intent intent) {
		 String action = intent.getAction();
		 // When discovery finds a device
		 if (BluetoothDevice.ACTION_FOUND.equals(action)) {
		 // Get the BluetoothDevice object from the Intent
		 BluetoothDevice device =
		 intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
		 
		// Add the name and address to an array adapter to show in a ListView
		 if(mArrayAdapter == null){
			 mArrayAdapter.add("leer");
		 }
		 mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
		 
		 }
		 }
		 };
		 // Register the BroadcastReceiver
		 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
		 registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
		 
		 
		
		// ####################################################

		lv1 = (ListView) findViewById(R.id.android_mylist);
		// By using setAdpater method in listview we an add string array in
		// list.
		lv1.setAdapter(mArrayAdapter);
//		lv1.setAdapter(new ArrayAdapter<String>(this,
//				android.R.layout.simple_list_item_1, lv_arr));
	}
}
